package com.dylan.历史.java8.StreamAPI;

import org.junit.Test;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Dylan on 2018/7/19.
 */
public class 创建Stream {

    @Test
    public void test1() throws IOException {
        // 集合创建Stream
        Stream<String> listStream = Arrays.asList("a", "b", "c", "d", "e", "f").stream();
        // 数组创建Stream
        Stream<String> stringStream1 = Stream.of("a", "b", "c", "d", "e", "f");
        // 数组创建Stream
        String[] arr = new String[]{"a", "b", "c", "d", "e", "f"};
        Stream<String> stringStream2 = Arrays.stream(arr);
        // 数组的一部分创建Stream
        Stream<String> stringStream3 = Arrays.stream(arr, 1, 3);
        // 创建一个不含任何元素的Stream
        Stream<String> stringStream4 = Stream.empty();
        // 使用generate创建无限Stream
        Stream<String> stringStream5 = Stream.generate(() -> "Echo");
        Stream<Double> stringStream6 = Stream.generate(Math::random);
        // 使用iterate创建无限Stream
        Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE));
        Stream.iterate(BigInteger.ZERO, UnaryOperator.identity());

        // 在用正则表达式分割时创建Stream
        Stream<String> stringStream7 = Pattern.compile("[\\P{L}]+").splitAsStream("Ramesses VI was the fifth ruler of the Twentieth Dynasty of Egypt. He succeeded Ramesses V and reigned for about eight years in the mid-to-late 12th century BC before dying in his forties. Egypt lost control of its last strongholds in Canaan around the time of his reign. The pharaoh's power waned in Upper Egypt during his rule, while the high priest of Amun, Ramessesnakht, was turning Thebes in Upper Egypt into the religious capital and a second center of power on par with Pi-Ramesses in Lower Egypt, where the pharaoh resided. He was fond of cult statues of himself; more are known to portray him than any other Twentieth-Dynasty king after his father, Ramesses III. He usurped KV9, a tomb in the Valley of the Kings planned by and for Ramesses V, and had it enlarged and redecorated for himself. His mummy lay untouched for fewer than 20 years before pillagers damaged it.");

        // 创建Stream
        try (Stream<String> lines = Files.lines(Paths.get("/Users/ankang/Downloads", "doctor_123_201807161551.csv"))) {
            lines.findFirst();
        }

    }
}
